﻿#ifndef PST_CURVE2DPLOTOR_H
#define PST_CURVE2DPLOTOR_H

#include "qcustomplot.h"
#include <qtablewidget.h>
#include "PolarAxisAngular.h"
#include "InfomationCurve2DBasical.h"

namespace pst
{
    class LineChartTracer;
    class LineChartStraightLine;

    class Curve2DPlotor : public QCustomPlot
    {
        Q_OBJECT
    public:
        explicit Curve2DPlotor(QWidget* parent = nullptr);

    public:

        void setWindowTitleName(const QString& titleName);
        /// @brief 文件写出至工程文件
        QDomElement& writeToProjectFile(QDomDocument* doc, QDomElement* element, bool isdiso = false);

        /// @brief 从工程文件读入数据
        void readFromProjectFile(QDomElement* element, bool isdiso = false);

        LineChartStraightLine* getLineChartStraightLineByID(int lineID);

        void setXAxisTitleName(const QString& titleName);
        void setXAxisFontFamily(const QString& family);
        void setXAxisFontSize(int size);
        void setXAxisRangeMin(double min);
        void setXAxisRangeMax(double max);
        void setXAxisRangeStep(double step);

        void setYAxisTitleName(const QString& titleName);
        void setYAxisFontFamily(const QString& family);
        void setYAxisFontSize(int size);
        void setYAxisRangeMin(double min);
        void setYAxisRangeMax(double max);
        void setYAxisRangeStep(double step);

        bool GetIsShowPreviewTracer() const
        {
            return m_isShowPreviewTracer;
        }
        void SetIsShowPreviewTracer(bool isShowPreviewTracer)
        {
            m_isShowPreviewTracer = isShowPreviewTracer;
        }
        LineChartTracer* GetPreviewTracer() const
        {
            return m_previewTracer;
        }

        const QVector<LineChartStraightLine*>& GetLineArray() const
        {
            return m_lineArray;
        }

        void setPolarAxisAngular(PolarAxisAngular* polarAxisAngular)
        {
            m_polarAxisAngular = polarAxisAngular;
        }

        PolarAxisAngular* getPolarAxisAngular()
        {
            return m_polarAxisAngular;
        }

        void setInfoListCopy(QList<Info2DPointer>* infoList)
        {
            infoListCopy = infoList;
        }

        void removeMarkerSelect();
        void removeMarkerAll();
        void enableTracer();
        void disableTracer();

    public slots:

        //退出标记模式
        void Curve2DPlotor::slot_exitAddMarkerState();

    signals:
        void signal_updateSelectedGraph();
        void signal_removeSelectedGraph();
        void signal_removeAlldGraph();
        void signal_addTracer();
        void signal_removeSelectedTracer(const QList<LineChartTracer*>& selectTracers);
        void signal_removeAlldTracer(const QList<LineChartTracer*>& selectTracers);
        void signal_renameGraph();
        void signal_copyGraph();
        void signal_pasteGraph();
        void signal_showLocalScope(bool isEdit = false);
        void signal_exportMarkerData();

    protected:
        bool eventFilter(QObject* watched, QEvent* event) override;

    private slots:
        /// @brief 双击修改标题名称
        void titleDoubleClick(QMouseEvent* event);
        /// @brief 双击修改轴名称
        void axisLabelDoubleClick(QCPAxis* axis, QCPAxis::SelectablePart part);
        /// @brief 双击修改图例名称
        void legendDoubleClick(QCPLegend* legend, QCPAbstractLegendItem* item);
        void selectionChanged();
        void slot_mousePress();
        void mouseWheel();
        /// @brief 随机添加图形
        void addRandomGraph();
        /// @brief 移除选中的图形
        void removeSelectedGraph();
        /// @brief 移除所有图形
        void removeAllGraphs();
        /// @brief 右键菜单
        void contextMenuRequest(QPoint pos);
        /// @brief 移动图例
        void moveLegend();
        /// @brief 图形点击
        void graphClicked(QCPAbstractPlottable* plottable, int dataIndex);

        // void mousePressEvent(QMouseEvent* event) override;
        void onGraphClicked(QCPAbstractPlottable* plottable, int dataIndex, QMouseEvent* event);
        void onLegendClicked(QCPLegend* legend, QCPAbstractLegendItem* item, QMouseEvent* event);

    private:
        QVector<LineChartStraightLine*> m_lineArray;

        int m_lineChartStraightLineMaxID{-1};

        bool m_isShowPreviewTracer;
        LineChartTracer* m_previewTracer;

        // 标记为极坐标图
        PolarAxisAngular* m_polarAxisAngular{nullptr};

        QList<Info2DPointer>* infoListCopy = nullptr;
    };
} // namespace pst
#endif
